package com.chenailin.www.controller.knowledge;

import com.chenailin.www.model.dto.KnowledgeMemberDTO;
import com.chenailin.www.model.pojo.KnowledgeMember;
import com.chenailin.www.model.vo.ResultVO;
import com.chenailin.www.service.KnowledgeService;
import com.chenailin.www.serviceimpl.KnowledgeServiceImpl;
import com.chenailin.www.util.JsonUtil;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author evi
 */
@WebServlet("/knowledge/member/*")
public class KnowledgeMemberController extends HttpServlet {
    private final KnowledgeService knowledgeService = new KnowledgeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getRequestURI().substring(req.getContextPath().length());
        Long userId = (Long) req.getSession().getAttribute("userId");

        try {
            if (path.startsWith("/knowledge/member/list/")) {
                Long knowledgeId = Long.parseLong(path.substring("/knowledge/member/list/".length()));
                List<KnowledgeMember> members = knowledgeService.listMembers(knowledgeId, userId);
                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(members)));
            }
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, e.getMessage())));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long operatorId = (Long) req.getSession().getAttribute("userId");
        KnowledgeMemberDTO dto = JsonUtil.fromJson(IOUtils.toString(req.getReader()), KnowledgeMemberDTO.class);

        try {
            knowledgeService.addMember(dto, operatorId);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.success()));
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, e.getMessage())));
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getRequestURI().substring(req.getContextPath().length());
        Long operatorId = (Long) req.getSession().getAttribute("userId");

        try {
            if (path.startsWith("/knowledge/member/")) {
                String[] parts = path.substring("/knowledge/member/".length()).split("/");
                if (parts.length == 2) {
                    Long knowledgeId = Long.parseLong(parts[0]);
                    Long memberId = Long.parseLong(parts[1]);
                    knowledgeService.removeMember(knowledgeId, memberId, operatorId);
                    resp.getWriter().write(JsonUtil.toJson(ResultVO.success()));
                }
            }
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, e.getMessage())));
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long operatorId = (Long) req.getSession().getAttribute("userId");
        KnowledgeMemberDTO dto = JsonUtil.fromJson(IOUtils.toString(req.getReader()), KnowledgeMemberDTO.class);

        try {
            knowledgeService.updateMemberRole(dto, operatorId);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.success()));
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, e.getMessage())));
        }
    }

}